import {Animation, getset, stage} from './LayaLib.js'

/**重力系数 */
const GRAVITY = 1.8 

class RoleAni extends Animation {
    constructor(options) {
        super()
        var self = this
        //重写Animation的index，处理同一图集的动作
        getset(0, RoleAni.prototype, 'index', ()=> this._index,
            (value)=> {
                this._index = value
                if (this._frames) {
                    this.graphics = this._frames[value]
                    if(this.stat === 1 && value === 7) {
                        this._index = -1 //回到跑的第1帧
                    }
                }
            }
        )
        self.pivotX = 45
        self.pivotY = 67
        // 0-暂停 1-跑 2-跳
        self.stat = 1
        //角色踩在障碍上的水平位置
        self.skipTimes = 0

        self.x = options.x
        self.y = options.y
        self.limitX = options.limitX
        self.loadAtlas(options.skin)
        self.interval = options.speed
        self.options = options
    }
}

var _proto = RoleAni.prototype
Object.defineProperty(_proto, 'WIDTH', {value: 35})
Object.defineProperty(_proto, 'HEIGHT', {value: 65})

_proto.checkOver = function() {
    var self = this
    if(self.x <= 0 || self.y >= 360) {
        self.stop()
        self.clearTimer(self, self.checkOver)
        stage.event('GAME_OVER')
        console.log('game over!')
    }
}

_proto.run = function() {
    this.stat = 1
    this.play(0, true)
}

_proto.skip = function(vy, force) {
    var self = this
    //只在空中只能二级跳
    if((self.stat === 1 || force) && self.skipTimes < 2) {
        self.skipTimes++
        self.stat = 2
        self.vy = vy
        self.stop()
        self.index = 8
        self.isUp = false
        self.isDown = false
        
        self.timerLoop(30, self, self.skipLoop, null, true)
    }
}

_proto.skipLoop = function() {
    var self = this
    if(self.vy > 0 && !self.isUp) {
        self.isUp = true
        self.index = 9
    }else if(self.vy < 0 && !self.isDown) {
        self.isDown = true
        self.index = 10
    }
    
    self.y -= (self.vy -= GRAVITY)
    // if(self.y < self.HEIGHT) {
    //     self.y = self.HEIGHT
    //     //碰到天花板反弹
    //     if(self.vy > 0) self.vy = -self.vy
    // }else 
    if(self.y >= self.floor) {
        self.clearTimer(self, self.skipLoop)
        self.skipTimes = 0
        self.y = self.floor
        self.run()
    }
}

_proto.raiseSpeed = function() {
    if(this.interval > 30) {
        this.interval -= 10
        this.run()
    }
}

_proto.start = function() {
    var self = this
    self.x = self.options.x
    self.y = self.options.y
    self.floor = 0
    self.skipTimes = 0
    self.interval = self.options.speed
    self.run()
    self.frameLoop(1, self, self.checkOver)
}

export default new RoleAni({
        skin: './bin/res/roles/role0.json',
        speed: 40,
        limitX: 150,
        x: 80,
        y: 310
    }
)